using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using TrueCode.DependencyManager.BuildActions.Injection;


namespace TrueCode.DependencyManager.Services.InjectionPoints
{
  public class BaseClassByNameMethodProvider : IMethodProvider
  {
    private readonly IMethodProvider _defaultProvider;
    private readonly Type _baseType;
    private readonly MethodByNameProvider _provider;


    public BaseClassByNameMethodProvider( IMethodProvider defaultProvider, Type baseType, params IMethod[] methods )
    {
      _defaultProvider = defaultProvider;
      _baseType = baseType;
      _provider = new MethodByNameProvider( methods );
    }

    #region Implementation of IMethodsProvider
    public IEnumerable< MethodInfo > GetMethodsOf( Type type )
    {
      return _baseType.IsAssignableFrom( type )
               ? _provider.GetMethodsOf( type ).Concat( _defaultProvider.GetMethodsOf( type ) )
               : _defaultProvider.GetMethodsOf( type );
    }
    #endregion
  }

  public class BaseClassByNameMethodProvider< TBaseClass > : BaseClassByNameMethodProvider
  {
    public BaseClassByNameMethodProvider( IMethodProvider defaultProvider, params IMethod[] methods )
      : base( defaultProvider, typeof( TBaseClass ), methods )
    {
    }
  }
}